package com.wzg.security;

import org.junit.Test;
import com.wzg.security.HQCertificate;

/**
 * @author henryGui
 * @version V1.0
 * @Title: CertificateTest
 * @Package com.wzg.security
 * @Description: TODO
 * @date 2018-07-09 下午3:35
 */
public class CertificateTest {

    HQCertificate certificate = HQCertificate.getInstance();
    HQBase64 base64 = HQBase64.getInstance();

    private char[] password = "cas123".toCharArray();
    private String alias = "test";
    private String certificatePath = "/Users/wuzhenggui/work/IdeaProjects/mayun/spring-base-parent/java-key-security-demo/src/main/resources/test.crt";
    private String keyStorePath = "/Users/wuzhenggui/work/IdeaProjects/mayun/spring-base-parent/java-key-security-demo/src/main/resources/test.keystore";

    @Test
    public void encode() throws Exception
    {
        byte[] data = "nihao,zhenggui好".getBytes();
        HQCertificate.HQKeyStore keyStore = HQCertificate.HQKeyStore.JKS;
        System.err.println("验证证书：" + certificate.verifyCertificate(certificatePath));
        byte[] signResult = certificate.sign(data, keyStorePath, alias, password, keyStore);
        System.err.println("签名：" + base64.encodeToString(signResult));
        System.err.println("验签：" + certificate.verify(data, signResult, certificatePath));

        byte[] result = certificate.encrypt(data, keyStorePath, alias, password, HQCertificate.HQKeyStore.JKS);
        System.err.println("加密：" + base64.encodeToString(signResult));
        System.err.println("解密：" + new String(certificate.decrypt(result, certificatePath)));
    }

}
